// 1.typeof: 使用的最多
function printID(id: number | string) {
  if (typeof id === 'string') {
    console.log(id.length, id.split(" "));
  } else {
    console.log(id.toString)
  }
}

// 2.===/!==: 方向的类型判断
type Direction = "left" | "right" | "up" | "down"
function switchDirection(direction: Direction) {
  if (direction === "left") {
    console.log("左:", "角色向左移动")
  } else if (direction === "right") {
    console.log("右:", "角色向右移动")
  } else if (direction === "up") {
    console.log("上:", "角色向上移动")
  } else if (direction === "down") {
    console.log("下:", "角色向下移动")
  }
}


// 3. instanceof: 传入一个日期, 打印日期
function printDate(date: string | Date) {
  if (date instanceof Date) {
    console.log(date.getTime())
  } else {
    console.log(date)
  }

  // if (typeof date === "string") {
  //   console.log(date)
  // } else {
  //   console.log(date.getTime())
  // }
}

// 4.in: 判断是否有某一个属性
interface Person {
  name?: string;
  swim: () => void;
}

interface Animal {
  name?: string;
  runing: () => void;
}

function printInfo(info: Person | Animal) {
  if ("species" in info) {
    console.log(info.species)
  } else if ("runing" in info) {
    console.log(info.runing)
  } else if ("swim" in info) {
    console.log(info.swim)
  }
}

const fish: Person = {
  swim: function() {}
}

const dog: Animal = {
  runing: function() {}
}

printInfo(fish)
printInfo(dog)

export {}